<template>
  <el-dialog
    v-model="dialogVisible"
    title="订单详情"
    width="800px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <div class="order-detail-content" v-if="orderDetail">
      <!-- 基本信息 -->
      <el-card class="detail-card" shadow="never">
        <div class="detail-card-header">
          <h4>基本信息</h4>
          <el-tag :type="getStatusType(orderDetail.orderStatus)" size="small">
            {{ getStatusText(orderDetail.orderStatus) }}
          </el-tag>
        </div>
        <div class="detail-item">
          <span class="detail-label">订单编号:</span>
          <span class="detail-value">{{ orderDetail.orderNumber }}</span>
        </div>
        <div class="detail-item">
          <span class="detail-label">创建时间:</span>
          <span class="detail-value">{{ formatDate(orderDetail.createdAt) }}</span>
        </div>
        <div class="detail-item">
          <span class="detail-label">支付方式:</span>
          <span class="detail-value">{{ orderDetail.paymentMethod }}</span>
        </div>
        <div class="detail-item" v-if="orderDetail.remark">
          <span class="detail-label">订单备注:</span>
          <span class="detail-value">{{ orderDetail.remark }}</span>
        </div>
      </el-card>

      <!-- 发件人信息 -->
      <el-card class="detail-card" shadow="never">
        <div class="detail-card-header">
          <h4>发件人信息</h4>
        </div>
        <div class="detail-item">
          <span class="detail-label">姓名:</span>
          <span class="detail-value">{{ orderDetail.senderName }}</span>
        </div>
        <div class="detail-item">
          <span class="detail-label">手机:</span>
          <span class="detail-value">{{ orderDetail.senderPhone }}</span>
        </div>
        <div class="detail-item">
          <span class="detail-label">地址:</span>
          <span class="detail-value">{{ orderDetail.senderAddress }}</span>
        </div>
      </el-card>

      <!-- 收件人信息 -->
      <el-card class="detail-card" shadow="never">
        <div class="detail-card-header">
          <h4>收件人信息</h4>
        </div>
        <div class="detail-item">
          <span class="detail-label">姓名:</span>
          <span class="detail-value">{{ orderDetail.receiverName }}</span>
        </div>
        <div class="detail-item">
          <span class="detail-label">手机:</span>
          <span class="detail-value">{{ orderDetail.receiverPhone }}</span>
        </div>
        <div class="detail-item">
          <span class="detail-label">地址:</span>
          <span class="detail-value">{{ orderDetail.receiverAddress }}</span>
        </div>
      </el-card>

      <!-- 物品信息 -->
      <el-card class="detail-card" shadow="never">
        <div class="detail-card-header">
          <h4>物品信息</h4>
        </div>
        <div class="detail-item">
          <span class="detail-label">重量:</span>
          <span class="detail-value">{{ orderDetail.itemWeight }}kg</span>
        </div>
        <div class="detail-item" v-if="orderDetail.itemVolume">
          <span class="detail-label">体积:</span>
          <span class="detail-value">{{ orderDetail.itemVolume }}m³</span>
        </div>
        <div class="detail-item" v-if="orderDetail.itemType">
          <span class="detail-label">物品类型:</span>
          <span class="detail-value">{{ orderDetail.itemType }}</span>
        </div>
        <div class="detail-item" v-if="orderDetail.insuredValue">
          <span class="detail-label">保价金额:</span>
          <span class="detail-value">¥{{ orderDetail.insuredValue }}</span>
        </div>
      </el-card>

      <!-- 费用信息 -->
      <el-card class="detail-card" shadow="never">
        <div class="detail-card-header">
          <h4>费用信息</h4>
        </div>
        <div class="detail-item" v-if="orderDetail.baseFee">
          <span class="detail-label">基础费用:</span>
          <span class="detail-value">¥{{ orderDetail.baseFee }}</span>
        </div>
        <div class="detail-item" v-if="orderDetail.weightFee">
          <span class="detail-label">重量费用:</span>
          <span class="detail-value">¥{{ orderDetail.weightFee }}</span>
        </div>
        <div class="detail-item" v-if="orderDetail.serviceFee">
          <span class="detail-label">服务费用:</span>
          <span class="detail-value">¥{{ orderDetail.serviceFee }}</span>
        </div>
        <div class="detail-item" v-if="orderDetail.insuranceFee">
          <span class="detail-label">保险费用:</span>
          <span class="detail-value">¥{{ orderDetail.insuranceFee }}</span>
        </div>
        <div class="detail-item total-fee">
          <span class="detail-label">总费用:</span>
          <span class="detail-value total-amount">¥{{ orderDetail.totalFee }}</span>
        </div>
      </el-card>

      <!-- 配送员信息 -->
      <el-card class="detail-card" shadow="never" v-if="orderDetail.delivererId">
        <div class="detail-card-header">
          <h4>配送员信息</h4>
        </div>
        <div class="detail-item">
          <span class="detail-label">配送员ID:</span>
          <span class="detail-value">{{ orderDetail.delivererId || '暂未分配' }}</span>
        </div>
      </el-card>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button v-if="showSignButton && orderDetail?.orderStatus === 3" type="primary" @click="handleSign">
          确认签收
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import type { DeliveryOrder } from '@/api/user'

interface Props {
  visible: boolean
  orderDetail: DeliveryOrder | null
  showSignButton?: boolean
}

interface Emits {
  (e: 'update:visible', value: boolean): void
  (e: 'close'): void
  (e: 'sign', orderId: number): void
}

const props = withDefaults(defineProps<Props>(), {
  showSignButton: false
})

const emit = defineEmits<Emits>()

const dialogVisible = computed({
  get: () => props.visible,
  set: (value) => emit('update:visible', value)
})

const getStatusText = (status: number): string => {
  const statusMap: Record<number, string> = {
    1: '已下单',
    2: '待揽件',
    3: '已揽件',
    4: '运输中',
    5: '已到达',
    6: '已签收'
  }
  return statusMap[status] || '未知状态'
}

const getStatusType = (status: number): 'info' | 'primary' | 'warning' | 'success' | 'danger' => {
  const typeMap: Record<number, 'info' | 'primary' | 'warning' | 'success' | 'danger'> = {
    1: 'info',
    2: 'primary',
    3: 'primary',
    4: 'warning',
    5: 'success',
    6: 'danger'
  }
  return typeMap[status] || 'info'
}

// 移除未使用的getPaymentText函数

const formatDate = (dateString: string): string => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN')
}

const handleClose = () => {
  emit('close')
}

const handleSign = () => {
  if (props.orderDetail) {
    emit('sign', props.orderDetail.deliveryOrderId)
  }
}
</script>

<style scoped>
.order-detail-content {
  max-height: 600px;
  overflow-y: auto;
}

.detail-card {
  margin-bottom: 16px;
  border: 1px solid #e4e7ed;
}

.detail-card:last-child {
  margin-bottom: 0;
}

.detail-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.detail-card-header h4 {
  margin: 0;
  color: #2c3e50;
  font-size: 16px;
  font-weight: 600;
}

.detail-item {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  min-height: 24px;
}

.detail-item:last-child {
  margin-bottom: 0;
}

.detail-label {
  color: #909399;
  font-size: 14px;
  min-width: 80px;
  flex-shrink: 0;
}

.detail-value {
  color: #606266;
  font-size: 14px;
  flex: 1;
  word-break: break-all;
}

.total-fee .detail-label,
.total-fee .detail-value {
  font-weight: 600;
  font-size: 16px;
}

.total-amount {
  color: #e6a23c;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

@media (max-width: 768px) {
  .order-detail-content {
    max-height: 500px;
  }
  
  .dialog-footer {
    flex-direction: column;
  }
  
  .dialog-footer .el-button {
    width: 100%;
  }
  
  .detail-item {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .detail-label {
    margin-bottom: 4px;
    min-width: auto;
  }
}
</style> 